//
//  Floats.swift
//  JXBaseTools
//
//  Created by godox on 2022/6/30.
//

import Foundation


extension Float {
    
    /// 准确的小数尾截取 - 没有进位
    public func decimalString(_ base: Self = 1) -> String {
        let tempCount: Self = pow(10, base)
        let temp = self*tempCount
        let target = Self(Int(temp))
        let stepone = target/tempCount
        if stepone.truncatingRemainder(dividingBy: 1) == 0 {
            return String(format: "%.1f", stepone)
        }else{
            return "\(stepone)"
        }
    }
    
    public var toInt: Int {
        return Int(self)
    }
}

extension Double {

    public func roundTo(places:Int) -> Double {

        let divisor = pow(10.0, Double(places))

        return (self * divisor).rounded() / divisor

    }

}
